package com.example.websocketdemo.config;

import com.example.websocketdemo.hander.VideoOnlineHandler;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

@Configuration
@EnableWebSocket // 启用WebSocket支持
public class WebSocketConfig implements WebSocketConfigurer {

    private final VideoOnlineHandler videoOnlineHandler;

    // 构造函数注入我们的WebSocket处理器
    public WebSocketConfig(VideoOnlineHandler videoOnlineHandler) {
        this.videoOnlineHandler = videoOnlineHandler;
    }

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        // 注册一个WebSocket端点，路径为 "/ws/video"
        // .setAllowedOrigins("*") 允许所有来源的跨域请求（开发环境用，生产环境请指定具体域名）
        registry.addHandler(videoOnlineHandler, "/ws/video")
                .setAllowedOrigins("*");
    }
}
